<template>
  <el-dialog
    title="分配角色"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
      <!-- <el-checkbox label="复选框 B" />
      <el-checkbox label="复选框 C" /> -->
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button
          v-loading="loading"
          type="primary"
          size="small"
          @click="addRole"
        >确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { assignRolesApi } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: [],
      loading: false
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogVisible', false)
    },
    async addRole() {
      try {
        this.loading = true
        await assignRolesApi({ id: this.$attrs['user-id'], roleIds: this.roleIds })
        this.$message.success('分配成功')
        this.handleClose()
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
